$(function() {
	var result = []; //逾期的数据复制
	var repayment=[]; //还款的数据复制
	var copy = []; //逾期的数组json复制
	var repay_copy = []; //还款的数组json复制
	var overdue_group = document.getElementById('overdue-group');
	var anchor_list = [];
	//获取逾期的数据
	$.ajax({
		type: "get",
		url: "js/cuishou.json",
		async: false,
		success: function(res) {
			//进行排序，预防混乱
			function Sorts(a, b) {
				return a.initials.charCodeAt(0) - b.initials.charCodeAt(0);
			}
			res.data.sort(Sorts);
			copy = $.extend(true, [], copy, res.data);
			result = res;
			searchof(copy);
		}
	});
	$.ajax({
		type: "get",
		url: "js/replay.json",
		async: false,
		success: function(res) {
			//进行排序，预防混乱
			function Sorts(a, b) {
				return a.initials.charCodeAt(0) - b.initials.charCodeAt(0);
			}
			res.data.sort(Sorts);
			repay_copy = $.extend(true, [], copy, res.data);
			repayment = res;
			searchof(repay_copy);
		}
	});
	$('.overdue-box').append('<p class="overdueAll-data">' + result.all_num + '位逾期/逾期总金额' + result.all_amount + '元</p>');
	$('.repayment-box').append('<p class="repayment-data">' + result.all_num + '位还款/还款总金额' + result.all_amount + '元</p>')
	var str = '';
	$.each(anchor_list, function(index) {
		str += '<a href="javascript:void(0)">' + anchor_list[index] + '</a>'

	});
	
	$('.anchor-list').append(str).css('margin-top', -(anchor_list.length * 20 / 2));
	//创建dom
	function establish(datas, type) {
		var mark = 1;

		for(var i = 0; i < document.getElementsByClassName('overdue-tit').length; i++) {
			if(document.getElementsByClassName('overdue-tit')[i].innerText == type) {
				mark = 0;
			}
		}
		if(mark) {
			var overdue_tit = document.createElement('li');
			overdue_tit.className = "overdue-tit";
			overdue_tit.setAttribute('data-capital', type);
			overdue_tit.innerText = datas.initials;
			overdue_group.appendChild(overdue_tit);
		}

		var overdue_list = document.createElement('li');
		overdue_list.className = "overdue-list";
		overdue_list.setAttribute('data-name', datas.name);
		overdue_list.setAttribute('data-phone', datas.phone);
		var str = '';
		str += '<div class="flex-1"><img class="head-portrait" src="' + datas.head_portrait + '" /></div>';
		str += '<div class="flex-2"><p class="overdue-name">' + datas.name + '</p><p class="overdue-phone"><i class="phone-icon"></i>' + datas.phone + '</p></div>';
		str += '<div class="flex-1"><p class="voerdue-money">' + datas.amount + '</p></div>';
		str += '<div class="flex-1"><p class="voerdue-state">逾期<em>' + Math.floor(parseInt(datas.overdue_days)) + '</em>天</p></div>';
		overdue_list.innerHTML = str;
		overdue_group.appendChild(overdue_list);
	}

	function searchof(copy) {
		$.each(copy, function(i) {
			switch($(this)[0].initials) {
				case 'A':
					establish($(this)[0], $(this)[0].initials);
					if($.inArray($(this)[0].initials, anchor_list) < 0) {
						anchor_list.push($(this)[0].initials)
					}

					break;
				case 'B':
					establish($(this)[0], $(this)[0].initials);
					if($.inArray($(this)[0].initials, anchor_list) < 0) {
						anchor_list.push($(this)[0].initials)
					}
					break;
				case 'C':
					establish($(this)[0], $(this)[0].initials);
					if($.inArray($(this)[0].initials, anchor_list) < 0) {
						anchor_list.push($(this)[0].initials)
					}
					break;
				case 'D':
					establish($(this)[0], $(this)[0].initials);
					if($.inArray($(this)[0].initials, anchor_list) < 0) {
						anchor_list.push($(this)[0].initials)
					}
					break;
				case 'E':
					establish($(this)[0], $(this)[0].initials);
					if($.inArray($(this)[0].initials, anchor_list) < 0) {
						anchor_list.push($(this)[0].initials)
					}
					break;
				case 'F':
					establish($(this)[0], $(this)[0].initials);
					if($.inArray($(this)[0].initials, anchor_list) < 0) {
						anchor_list.push($(this)[0].initials)
					}
					break;
				case 'G':
					establish($(this)[0], $(this)[0].initials);
					if($.inArray($(this)[0].initials, anchor_list) < 0) {
						anchor_list.push($(this)[0].initials)
					}
					break;
				case 'H':
					establish($(this)[0], $(this)[0].initials);
					if($.inArray($(this)[0].initials, anchor_list) < 0) {
						anchor_list.push($(this)[0].initials)
					}
					break;
				case 'I':
					establish($(this)[0], $(this)[0].initials);
					if($.inArray($(this)[0].initials, anchor_list) < 0) {
						anchor_list.push($(this)[0].initials)
					}
					break;
				case 'J':
					establish($(this)[0], $(this)[0].initials);
					if($.inArray($(this)[0].initials, anchor_list) < 0) {
						anchor_list.push($(this)[0].initials)
					}
					break;
				case 'K':
					establish($(this)[0], $(this)[0].initials);
					if($.inArray($(this)[0].initials, anchor_list) < 0) {
						anchor_list.push($(this)[0].initials)
					}
					break;
				case 'L':
					establish($(this)[0], $(this)[0].initials);
					if($.inArray($(this)[0].initials, anchor_list) < 0) {
						anchor_list.push($(this)[0].initials)
					}
					break;
				case 'M':
					establish($(this)[0], $(this)[0].initials);
					if($.inArray($(this)[0].initials, anchor_list) < 0) {
						anchor_list.push($(this)[0].initials)
					}
					break;
				case 'N':
					establish($(this)[0], $(this)[0].initials);
					if($.inArray($(this)[0].initials, anchor_list) < 0) {
						anchor_list.push($(this)[0].initials)
					}
					break;
				case 'O':
					establish($(this)[0], $(this)[0].initials);
					if($.inArray($(this)[0].initials, anchor_list) < 0) {
						anchor_list.push($(this)[0].initials)
					}
					break;
				case 'P':
					establish($(this)[0], $(this)[0].initials);
					if($.inArray($(this)[0].initials, anchor_list) < 0) {
						anchor_list.push($(this)[0].initials)
					}
					break;
				case 'Q':
					establish($(this)[0], $(this)[0].initials);
					if($.inArray($(this)[0].initials, anchor_list) < 0) {
						anchor_list.push($(this)[0].initials)
					}
					break;
				case 'R':
					establish($(this)[0], $(this)[0].initials);
					if($.inArray($(this)[0].initials, anchor_list) < 0) {
						anchor_list.push($(this)[0].initials)
					}
					break;
				case 'S':
					establish($(this)[0], $(this)[0].initials);
					if($.inArray($(this)[0].initials, anchor_list) < 0) {
						anchor_list.push($(this)[0].initials)
					}
					break;
				case 'T':
					establish($(this)[0], $(this)[0].initials);
					if($.inArray($(this)[0].initials, anchor_list) < 0) {
						anchor_list.push($(this)[0].initials)
					}
					break;
				case 'U':
					establish($(this)[0], $(this)[0].initials);
					if($.inArray($(this)[0].initials, anchor_list) < 0) {
						anchor_list.push($(this)[0].initials)
					}
					break;
				case 'V':
					establish($(this)[0], $(this)[0].initials);
					if($.inArray($(this)[0].initials, anchor_list) < 0) {
						anchor_list.push($(this)[0].initials)
					}
					break;
				case 'W':
					establish($(this)[0], $(this)[0].initials);
					if($.inArray($(this)[0].initials, anchor_list) < 0) {
						anchor_list.push($(this)[0].initials)
					}
					break;
				case 'X':
					establish($(this)[0], $(this)[0].initials);
					if($.inArray($(this)[0].initials, anchor_list) < 0) {
						anchor_list.push($(this)[0].initials)
					}
					break;
				case 'Y':
					establish($(this)[0], $(this)[0].initials);
					if($.inArray($(this)[0].initials, anchor_list) < 0) {
						anchor_list.push($(this)[0].initials)
					}
					break;
				case 'Z':
					establish($(this)[0], $(this)[0].initials);
					if($.inArray($(this)[0].initials, anchor_list) < 0) {
						anchor_list.push($(this)[0].initials)
					}
					break;
				default:
					alert('数据异常！！！，检查json数据');
			}
		});
	}

	// 搜索框事件
	var search = document.getElementsByClassName('mui-input-clear')[0];
	var pot = 0
	search.addEventListener("keyup", function() {
		if(this.value == '') {
			if(pot) {
				$("#overdue-group").html('');
				searchof(copy);
				pot = 0;
			}
		} else if(isNaN(parseInt(this.value))) {
			var value = this.value;
			var name = [];
			pot = 1;
			$.each(copy, function(index) {
				if($(this)[0].name.indexOf(value) >= 0) {
					name.push($(this)[0]);
				}

			});
			$("#overdue-group").html('');
			searchof(name);
			name.splice(0, name.length)
		} else if(typeof parseInt(this.value) == 'number') {
			var value = this.value;
			var phone = [];
			pot = 1;
			$.each(copy, function(index) {
				if($(this)[0].phone.indexOf(value) >= 0) {
					phone.push($(this)[0]);
				}

			});
			$("#overdue-group").html('');
			searchof(phone);
			phone.splice(0, phone.length)
		} else {
			alert('程序出问题')
		}

	});
	//快捷定位事件
	var offset_top = 0;
	$(".anchor-list a").click(function() {
		var val = $(this).text();
		$('.anchor-show').show().text(val);
		setTimeout(function() {
			$('.anchor-show').hide().text('')
		}, 400);

		$('.overdue-tit').each(function(i) {
			if($(this).text() == val) {
				if($(".overdue-content").scrollTop() <= $('.overdue-box').height() - $(".overdue-content").height()) {
					offset_top += parseInt($(this).offset().top) - 52;
					if(offset_top > $('.overdue-box').height() - $(".overdue-content").height()) {
						offset_top = $('.overdue-box').height() - $(".overdue-content").height()
					}
				}
				$(".overdue-content").scrollTop(offset_top);
			}
		})
	})
});